package com.xauto.pioneer.appuser.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.xauto.pioneer.appuser.R;
import com.xauto.pioneer.appuser.adapter.ParkingSiteAdapter;
import com.xauto.pioneer.appuser.http.BaseLHttpHandler;
import com.xauto.pioneer.appuser.http.Constants;
import com.xauto.pioneer.appuser.http.LHttpLib;
import com.xauto.pioneer.appuser.model.JSONStatus;
import com.xauto.pioneer.appuser.model.PageInfo;
import com.xauto.pioneer.appuser.model.ParkingSite;
import com.xauto.pioneer.appuser.view.TopBar;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by Libo on 2018-03-19.
 */

public class ParkingSiteActivity extends BaseActivity {

    @Bind(R.id.topBar)
    TopBar topBar;
    @Bind(R.id.list)
    PullToRefreshListView pullToRefreshListView;
    private ListView list;
    /** 取车列表数据适配器*/
    private ParkingSiteAdapter adapter;
    /** 取车网点数据集合*/
    private List<ParkingSite> parkingSites;

    private String longitude;
    private String latitude;
    private String distance;
    @Override
    protected int getContentView() {
        return R.layout.ac_list_no_refresh;
    }

    @Override
    protected void initView() {
        topBar.showView(TopBar.LEFT_IMAGE_BTN, TopBar.TITLE);
        topBar.setText(TopBar.TITLE, getString(R.string.top_bar_parking_site));
        topBar.setImage(TopBar.LEFT_IMAGE_BTN, R.mipmap.ic_common_left);
        topBar.setLeftOnClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
        list = pullToRefreshListView.getRefreshableView();
        list.setVerticalScrollBarEnabled(false);
        list.setDividerHeight(3);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //当前点击网点
                ParkingSite currentParkingSite =  adapter.getItem(position-1);
                Intent intent = new Intent();
                intent.putExtra("parkingSite",currentParkingSite);
                setResult(RESULT_OK,intent);
                finish();

            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                //刷新
                page = 1;
                load();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                //下拉
                if (isMore){
                    page ++;
                    load();
                }else {
                    pullToRefreshListView.onRefreshComplete();
                    prompt("暂无更多数据");
                }
            }
        });
    }

    @Override
    protected void initData() {
        longitude = getIntent().getStringExtra("longitude");
        latitude = getIntent().getStringExtra("latitude");
        distance = getIntent().getStringExtra("distance");

        lHandler = new BaseLHttpHandler(mContext, true, true) {
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                pullToRefreshListView.onRefreshComplete();
            }

            @Override
            public void onSuccess(JSONStatus jsonStatus) {
                super.onSuccess(jsonStatus);
                pullToRefreshListView.onRefreshComplete();
                JSONArray jsonArray = jsonStatus.data.optJSONArray("parking_site_list");
                isMore = jsonStatus.pageInfo.more == 1 ? true : false;
                if(jsonArray != null && jsonArray.length() > 0) {
                    parkingSites = new ArrayList<>();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        parkingSites.add(new ParkingSite().parse(jsonArray.optJSONObject(i)));
                    }
                    refreshList();
                }else {
                    showNoDataView();
                }
            }
        };

        adapter = new ParkingSiteAdapter(this, R.layout.item_parking_site);
        list.setAdapter(adapter);
        page = 1;
        load();
    }

    void load() {
        new LHttpLib().getPickParkingSiteList("",mContext,  longitude, latitude, distance, new PageInfo(page, Constants.COUNT), lHandler);
    }

    void refreshList() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (page == 1){adapter.getDataList().clear();}
                adapter.getDataList().addAll(parkingSites);
                adapter.notifyDataSetChanged();
            }
        });
    }
    private void showNoDataView(){
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.layout_listview_empty, null);
        ((TextView) emptyView.findViewById(R.id.tvEmptyNotice)).setText("当前无取车网点信息");
        ((ViewGroup)list.getParent()).addView(emptyView);

        list.setEmptyView(emptyView);
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}